using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using WP.Commons;
using WP.Controls;
using WP.DB;
using WP.Modules;
using WP.MF.BusinessLogical;
using WP.MF.BusinessRule;
using WP.MF.DataAccess;

namespace WP.MF.Form
{
    public partial class frmMUserCopy : BaseQryRpt
    {
        public string m_strName = string.Empty;
        private bool m_bCanTrigerFieldValid = true;

        public frmMUserCopy()
        {
            InitializeComponent();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            MUserBLL mBll = new MUserBLL();
            if (!mBll.CopyMUser(m_strName, txtName.Text, txtDesc.Text)) { return; }

            txtName.Text = string.Empty;
            txtDesc.Text = string.Empty;
            m_bCanTrigerFieldValid = false;
            txtName.Focus();
            m_bCanTrigerFieldValid = true;
        }
        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void btnHelp_Click(object sender, EventArgs e)
        {
            base.OnHelp();
        }
        private void Field_Validating(object sender, CancelEventArgs e)
        {
            Control control = sender as Control;
            string strErrPrompt = "";

            if (!m_bCanTrigerFieldValid)
            {
                e.Cancel = false;
                this.errProvider.SetError(control, "");
                return;
            }

            if (!FieldCheck(control.Text, control.Name.Substring(3), ref  strErrPrompt))
            {
                errProvider.SetIconAlignment(control, ErrorIconAlignment.MiddleRight);
                this.errProvider.SetError(control, strErrPrompt);
                if (strErrPrompt.Trim() != "") MsgBoxEx.Show(strErrPrompt, SysVar.WarnTitle);
                e.Cancel = true;
            }
            else
            {
                if (strErrPrompt.Trim() != "") MsgBoxEx.Show(strErrPrompt, SysVar.WarnTitle);
                this.errProvider.SetError(control, "");
            }

        }

        private bool FieldCheck(string strFieldValue, string strFieldName, ref string strErrPrompt)
        {
            strErrPrompt = string.Empty;
            DBAccess dBAccess = new DBAccess(false, false);

            MUserBRL mBRL = new MUserBRL();
            bool bCheckResult = mBRL.CopyFieldCheck(dBAccess, strFieldName, strFieldValue, ref strErrPrompt);
            dBAccess.CloseCon();
            return bCheckResult;
        }
    }
}